﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace FormsDemo1
{
    public partial class BookManagerForm : Form
    {
        public BookManagerForm()
        {
            InitializeComponent();
        }

        private void BookManagerForm_Load(object sender, EventArgs e)
        {
            Table();
        }
        //从数据库中读取数据显示在表格控件中
        public void Table()
        {
            dgv_booklist.Rows.Clear();
            DatabaseData dbd = new DatabaseData();
            IDataReader reader = dbd.read("SELECT ID,NAME,AUTHOR,NUMBER,PRESS " +
                "FROM BOOK");
            while (reader.Read())
            {
                dgv_booklist.Rows.Add(reader[0].ToString(),reader[1].ToString(),
                    reader[2].ToString(), Convert.ToInt32(reader[3]), reader[4].ToString());
            }
            //关闭资源
            reader.Close();
            dbd.close();
        }
        //新增操作
        private void button1_Click(object sender, EventArgs e)
        {
            AddUpdateBookForm.tag = 0;
            AddUpdateBookForm aubf = new AddUpdateBookForm();
            aubf.refresh += refreshForm;
            aubf.ShowDialog();
        }

        public void refreshForm() {
            this.Table();
        }
        //修改按钮操作
        private void button2_Click(object sender, EventArgs e)
        {
            AddUpdateBookForm.tag = 1;
            AddUpdateBookForm aubf = new AddUpdateBookForm();
            try
            {
                aubf.refresh += refreshForm;
                aubf.bindData
                    (
                    dgv_booklist.SelectedRows[0].Cells[0].Value.ToString(),
                    dgv_booklist.SelectedRows[0].Cells[1].Value.ToString(),
                    dgv_booklist.SelectedRows[0].Cells[2].Value.ToString(),
                    dgv_booklist.SelectedRows[0].Cells[3].Value.ToString(),
                    dgv_booklist.SelectedRows[0].Cells[4].Value.ToString()
                    );           
            }
            catch (Exception)
            {

                MessageBox.Show("请选择一行再进行操作");
            }
            aubf.ShowDialog();
        }
        //删除图书
        private void button3_Click(object sender, EventArgs e)
        {

        }
    }
}
